import {
  Body,
  Controller,
  Get,
  HttpCode,
  HttpStatus,
  Post,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { SignInDto } from './dto/sign-in.dto';
import { Public } from '../../../common/decorator/my.decorator';
import { UserService } from '../user/user.service';
import { MenuService } from '../menu/menu.service';
import { RoleEnum } from '../../../util/enum/role.enum';
import { ReqUser } from '../../../common/decorator/user.decorator';

@Controller()
export class AuthController {
  constructor(
    private readonly authService: AuthService,
    private readonly userService: UserService,
    private readonly menuService: MenuService,
  ) {}

  /**
   * 登录
   * @param signInDto
   */
  @Public()
  @HttpCode(HttpStatus.OK)
  @Post('login')
  signIn(@Body() signInDto: SignInDto) {
    return this.authService.signIn(signInDto);
  }

  /**
   * 用户登录后,获取用户的roles和perms
   */
  @Get('me')
  getMe(@ReqUser('id') id: number) {
    return this.userService.findRolesAndPerms(id);
  }

  /**
   * 登录的时候生成左边的路由信息
   */
  @Get('routes')
  async getRoutes(@ReqUser('id') id: number) {
    const isRoot = await this.userService.hasOneOfRoles(id, [RoleEnum.Root]);
    return this.menuService.getRoutes(isRoot);
  }
}
